<template>
  <view>
    <view class="search-box">
      <my-search @click="gotoSearch"></my-search>
    </view>
    
    <view class="scroll-view-container">
      <scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh + 'px'}">
        <block v-for="(item, i) in cateList" :key="i">
          <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeCateChange(i)">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <scroll-view scroll-y="true" class="right-scroll-view" :style="{height: wh + 'px'}" :scroll-top="scrollTop">
        <view class="cate-v2-list" v-for="(item, i) in catev2List" :key="i">
          <view class="cate-v2-title">
            / {{item.cat_name}} /
          </view>
          <view class="cate-v2-img-list">
            <view class="cate-v2-img-item" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMixin from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMixin],
    data() {
      return {
        wh: 0,
        cateList: [],
        active: 0,
        catev2List: [],
        // 距离顶部的距离
        scrollTop: 0
      };
    },
    onLoad() {
      this.wh = uni.getSystemInfoSync().windowHeight -50
      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()

        this.cateList = res.message
        this.catev2List = this.cateList[0].children
      },
      activeCateChange(index) {
        this.active = index
        this.catev2List = this.cateList[index].children
        this.scrollTop = this.scrollTop ? 0 : 1
      },
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .scroll-view-container {
    display: flex;

    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        background-color: #f7f7f7;
        height: 60px;
        line-height: 60px;
        font-size: 12px;
        text-align: center;

        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            display: block;
            content: "";
            width: 3px;
            height: 30px;
            background-color: #c77777;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }

    .cate-v2-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }

    .cate-v2-img-list {
      display: flex;
      flex-wrap: wrap;

      .cate-v2-img-item {
        width: 33.3%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          margin: 5px 0;
          font-size: 12px;
        }
      }
    }
  }
</style>
